//
//  YHPStatusCell.swift
//  SinaSwift
//
//  Created by Yan on 16/3/15.
//  Copyright © 2016年 baidu. All rights reserved.
//

import UIKit
import SnapKit

class YHPStatusCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var bottomViewTopConstraints: Constraint?

    var status: YHPStatus? {
        didSet {
            originalView.status = status
            bottomViewTopConstraints?.uninstall()
            //有转发视图
            if let retweeted = status?.retweeted_status {
                retweetedView.retweetStatus  = retweeted
                bottomView.snp_updateConstraints(closure: { (make) in
                   self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                retweetedView.hidden = false
                
            } else {
                //解决cell复用问题
                retweetedView.retweetStatus = nil
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopConstraints = make.top.equalTo(originalView.snp_bottom).constraint
                })
                retweetedView.hidden = true
            }
        }
    }
    
    
    //懒加载属性
    /** 底部视图 */
    lazy var bottomView: YHPStatusBottomView = YHPStatusBottomView()
    /** 微博视图 */
    lazy var originalView : YHPStatusOriginalView = YHPStatusOriginalView()
    /** 转发微博视图 */
    lazy var retweetedView: YHPRetweetedView = YHPRetweetedView()

    
    func setupUI() {
        
        contentView.backgroundColor = UIColor(white: 0.99, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.contentView.snp_top)
            make.left.equalTo(self.contentView.snp_left)
            make.right.equalTo(self.contentView.snp_right)
            //设置左右约束上下约束和自动换行就是设置自动换行约束 不需要设置行高会和自动换行冲突
//            make.height.equalTo(100)
        }
        
        retweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(self.originalView)
        }

        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedView.snp_bottom)
            make.left.right.equalTo(self.contentView)
            self.bottomViewTopConstraints = make.bottom.equalTo(retweetedView.snp_bottom).constraint
            make.height.equalTo(36)
        }
        
        //开启自动布局之后 所有的frame布局就会自动失效
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }

}

